package org.openea.eap.module.obpm.util.custsql;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.openbpm.base.core.util.BeanUtils;
import org.openbpm.base.core.util.StringUtil;
import org.openea.eap.module.obpm.util.custsql.SqlUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class JSONObjectUtil {
    public static JSONObject getJSONObjectTrue() {
        JSONObject jo = new JSONObject();
        jo.put("success", "true");
        return jo;
    }

    public static JSONObject getJSONObjectTrue(String key, Object value) {
        JSONObject jo = new JSONObject();
        jo.put("success", "true");
        jo.put(key, value);
        return jo;
    }

    public static JSONObject getJSONObjectFalse() {
        JSONObject jo = new JSONObject();
        jo.put("success", "false");
        return jo;
    }

    public static JSONObject getJSONObjectFalse(String msg) {
        JSONObject jo = new JSONObject();
        jo.put("success", "false");
        jo.put("msg", msg);
        return jo;
    }

    public static JSONObject parseExpand(String expand) {
        JSONObject parseObject = null;
        try {
            parseObject = JSONObject.parseObject(expand);
        } catch (Exception e) {
            try {
                parseObject = JSONObject.parseObject(SqlUtil.resetQuoteStr(StringUtil.resetJsonStr(StringUtil.quotToStr(expand))));
            } catch (Exception e1) {
                try {
                    parseObject = JSONObject.parseObject(SqlUtil.resetQuoteStr(StringUtil.resetJsonStr(expand)));
                } catch (Exception e2) {
                    parseObject = JSONObject.parseObject(StringUtil.resetJsonStr(expand));
                }
            }
        }


        return parseObject;
    }


    public static String getValByKey(String key, String jsonStr, JSONObject jsonObj) {
        if (StringUtil.isEmpty(key) || StringUtil.isEmpty(jsonStr)) {
            return "";
        }
        if (jsonObj == null) {
            return "";
        }

        String[] keys = key.split("\\.");

        int len = keys.length;

        String curKey = keys[0];

        String curData = "";

        int arrIdx = 0;
        boolean isArray = false;
        if (curKey.indexOf("[") > 0) {
            String[] split = curKey.split("\\[");
            curKey = split[0];
            arrIdx = Integer.parseInt(split[1].replace("]", ""));
            isArray = true;
        }

        if (jsonObj.containsKey(curKey)) {
            curData = jsonObj.getString(curKey);
        }

        if (StringUtil.isEmpty(curData)) {
            return "";
        }

        if (len == 1) {
            return curData;
        }


        StringBuffer sb = new StringBuffer();

        for (int i = 1; i < keys.length; i++) {

            sb.append(keys[i]);

            if (i < keys.length) {
                sb.append(".");
            }
        }

        if (isArray) {
            try {
                curData = JSONArray.parseObject(curData).getString(arrIdx + "");
            } catch (Exception exception) {
            }
        }


        return getValByKey(sb.toString(), curData);
    }


    public static String getValByKey(String key, String jsonStr) {
        if (StringUtil.isEmpty(key) || StringUtil.isEmpty(jsonStr)) {
            return "";
        }
        JSONObject jsonObj = null;
        try {
            jsonObj = JSONObject.parseObject(StringUtil.resetJsonStr(jsonStr));
        } catch (Exception e) {
            jsonObj = JSONObject.parseObject(jsonStr);
        }

        if (BeanUtils.isEmpty(jsonObj)) {
            return "";
        }

        String[] keys = key.split("\\.");

        int len = keys.length;

        String curKey = keys[0];

        String curData = "";

        int arrIdx = 0;
        boolean isArray = false;
        if (curKey.indexOf("[") > 0) {
            String[] split = curKey.split("\\[");
            curKey = split[0];
            arrIdx = Integer.parseInt(split[1].replace("]", ""));
            isArray = true;
        }

        if (jsonObj.containsKey(curKey)) {
            curData = jsonObj.getString(curKey);
        }

        if (StringUtil.isEmpty(curData)) {
            return "";
        }

        if (len == 1) {
            return curData;
        }


        StringBuffer sb = new StringBuffer();

        for (int i = 1; i < keys.length; i++) {

            sb.append(keys[i]);

            if (i < keys.length) {
                sb.append(".");
            }
        }

        if (isArray) {
            try {
                curData = JSONArray.parseObject(curData).getString(arrIdx + "");
            } catch (Exception exception) {
            }
        }


        return getValByKey(sb.toString(), curData);
    }


    public static String toJsonStr(Object obj) {
        return JSON.toJSONString(obj);
    }


    public static String getValByKey(String key, JSONObject jsonObject) {
        if (BeanUtils.isEmpty(jsonObject) || jsonObject.isEmpty())
            return null;
        return getValByKey(key, jsonObject.toString());
    }

    public static JSONObject revertKeyAndVal(JSONObject jo) {
        if (BeanUtils.isNotEmpty(jo) && !jo.isEmpty()) {
            Set<String> keySet = jo.keySet();
            JSONObject revertJo = new JSONObject();
            for (String key : keySet) {
                String val = jo.getString(key);
                if (!StringUtil.isEmpty(val)) {
                    revertJo.put(val, key);
                }
            }
            return revertJo;
        }
        return new JSONObject();
    }


    public static <T> List<T> toList(String jsonArray, Class<T> t) {
        if (StringUtil.isEmpty(jsonArray)) {
            return null;
        }
        JSONArray buttonsArray = JSONArray.parseArray(jsonArray);
        List<T> objList = new ArrayList<>();
        for (int i = 0; i < buttonsArray.size(); i++) {
            JSONObject buttonJo = buttonsArray.getJSONObject(i);
            T obj = (T) buttonJo.toJavaObject(t);
            objList.add(obj);
        }
        return objList;
    }


    public static <T> String toListString(String jsonArray, Class<T> t) {
        if (StringUtil.isEmpty(jsonArray)) {
            return jsonArray;
        }
        JSONArray buttonsArray = JSONArray.parseArray(jsonArray);
        List<T> objList = new ArrayList<>();
        for (int i = 0; i < buttonsArray.size(); i++) {
            JSONObject buttonJo = buttonsArray.getJSONObject(i);
            T obj = (T) buttonJo.toJavaObject(t);
            objList.add(obj);
        }
        return JSONArray.toJSONString(objList);
    }
}
